package edu.boun.g4.coursity.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.boun.g4.coursity.domain.Course;
import edu.boun.g4.coursity.domain.Instructor;

/**
 * 
 * @author mertusta
 * @author bulutkorkmaz
 * @author alifindik
 * 
 */
@Service("instructorService")
@Transactional
public class InstructorService {

	protected static Logger logger = Logger.getLogger("service");

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/**
	 * Retrieves all instructors
	 * 
	 * @return a list of instructors
	 */
	@SuppressWarnings("unchecked")
	public List<Instructor> getAll() {
		logger.debug("Retrieving all instructors");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM  Instructor");

		// Retrieve all
		return query.list();
	}

	/**
	 * Retrieves instructors of a course
	 */
	public Instructor getAll(Integer courseId) {
		logger.debug("Retrieving instructors of the course");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session
				.createQuery("FROM Course as p LEFT JOIN FETCH  p.instructor WHERE p.id="
						+ courseId);

		Course course = (Course) query.uniqueResult();

		// Retrieve all
		return (course.getInstructor());
	}

	/**
	 * Retrieves a single instructor
	 */
	public Instructor get(Integer id) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing instructor first
		Instructor instructor = (Instructor) session.get(Instructor.class, id);

		return instructor;
	}

	/**
	 * Retrieves a single instructor by its info
	 */
	public Instructor getByInfo(String info) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM Instructor WHERE info='" + info
				+ "'");

		Instructor instructor = (Instructor) query.uniqueResult();

		return instructor;
	}

	/**
	 * Adds a new instructor
	 */
	public void add(Instructor instructor) {
		logger.debug("Adding new instructor");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Persists to db
		session.save(instructor);
	}

	/**
	 * Deletes an existing instructor
	 * 
	 * @param id
	 *            the id of the existing instructor
	 */
	public void delete(Integer id) {
		logger.debug("Deleting existing instructor");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing instructor first
		Instructor instructor = (Instructor) session.get(Instructor.class, id);

		// Delete
		session.delete(instructor);
	}

	/**
	 * Edits an existing initiative
	 */
	public void edit(Instructor instructor) {
		logger.debug("Editing existing instructor");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing instructor via id
		Instructor existingInstructor = (Instructor) session.get(
				Instructor.class, instructor.getInstructorId());

		// Assign updated values to this instructor
		existingInstructor.setInstructorName(instructor.getInstructorName());
		existingInstructor.setInfo(instructor.getInfo());
		// existingInstructor.setCourses(instructor.getCourses());

		// Save updates
		session.save(existingInstructor);
	}
}
